/-app
/-boot
/-imports
/-storage ...
/-storage/attached ...
/-storage/attached/api
/-storage/attached/indexedDB ...
DetectStorage.ts
FileData.ts
LoadStorage.ts
MetadataData.ts
UpdateStorage.ts
functions.ts
/-storage/attached/localStorage
/-storage/attached/webSQL
/-tests
/-typings
stringUtils.ts
teapo.html
1
module teapo.storage.attached.indexedDB {
2
 
3
  export class DetectStorage implements attached.DetectStorage {
4
 
5
    constructor(
6
      private _window: { indexedDB: IDBFactory; } = window) {
7
 
8
    }
9
 
10
    detectStorageAsync(uniqueKey: string, callback: (error: Error, load: LoadStorage) => void) {
11
 
12
      if (!this._window.indexedDB) {
13
        callback(new Error('No indexedDB object exposed from window.'), null);
14
        return;
15
      }
16
 
17
      if (typeof this._window.indexedDB.open !== 'function') {
18
        callback(new Error('No open method exposed on indexedDB object.'), null);
19
        return;
20
      }
21
 
22
      var dbName = uniqueKey || 'teapo';
23
 
24
      var openRequest = this._window.indexedDB.open(dbName, 1);
25
      openRequest.onerror = (errorEvent) => callback(wrapErrorEvent(errorEvent, 'detectStorageAsync-open'), null);
26
 
27
      openRequest.onupgradeneeded = (versionChangeEvent) => {
28
        var db: IDBDatabase = openRequest.result;
29
        var filesStore = db.createObjectStore('files', { keyPath: 'path' });
30
        var metadataStore = db.createObjectStore('metadata', { keyPath: 'property' });
31
      };
32
 
33
      openRequest.onsuccess = (event) => {
34
        var db: IDBDatabase = openRequest.result;
35
 
36
        var transaction = db.transaction('metadata');
37
        transaction.onerror = (errorEvent) => callback(wrapErrorEvent(errorEvent, 'detectStorageAsync-openRequest.onsuccess-transaction'), null);
38
 
39
        var metadataStore = transaction.objectStore('metadata');
40
 
41
        var editedUTCRequest = metadataStore.get('editedUTC');
42
        editedUTCRequest.onerror = (errorEvent) => {
43
          callback(null, new LoadStorage(null, db));
44
        };
45
 
46
        editedUTCRequest.onsuccess = (event) => {
47
          var result: MetadataData = editedUTCRequest.result;
48
          callback(null, new LoadStorage(result && typeof result.value === 'number' ? result.value : null, db));
49
        };
50
 
51
      };
52
 
53
    }
54
 
55
  }
56
 
57
}